<?php

namespace App\validate;

use think\Validate;
use think\facade\Db;

class OrderValidate extends Validate
{
    /**
     * 定义验证规则
     * 格式：'字段名' =>  ['规则1','规则2'...]
     *
     * @var array
     */
    protected $rule = [
        'uid' => 'require|checkUid',
        'goods_id' => 'require|checkGoodsId',
        'buy_count' => 'require|number',
        'pay_type' => 'require|in:sd_h5_checkstand,hf'
    ];

    /**
     * 定义错误信息
     * 格式：'字段名.规则名' =>  '错误信息'
     *
     * @var array
     */
    protected $message = [
        'uid.require' => '请传入用户id',
        'goods_id.require' => '请传入要购买的藏品',
        'buy_count.require' => '请传入购买数量',
        'buy_count.number' => '购买数量必须是数字',
        'pay_type.require' => '请选择支付类型',
        'pay_type.in' => '支付类型必须是 sd_h5_checkstand 或者是 hf',
    ];

    /**
     * @param $uid
     * @param $rule
     * @param $data
     * @return bool|string
     */
    public function checkUid($uid, $rule, $data = [])
    {
        $user = Db::name('user')->where('id', $uid)->find();
        if (empty($user)) {
            return '用户不存在';
        }
        return true;
    }

    /**
     * @param $goods_id
     * @return bool|string
     */
    public function checkGoodsId($goods_id)
    {
        $find = Db::name('goods')->where('id', $goods_id)->find();
        if (empty($find)) {
            return '藏品id不存在';
        }
        return true;
    }

    /**
     * @var \string[][]
     */
    protected $scene = [
        'create_order' => ['uid', 'goods_id', 'buy_count', 'pay_type']
    ];
}
